const models = require('../../models/models');
class student{

    list(req, res){
        let page = parseInt(req.query.page);
        page = page ? page : 1;
        let pageSize = parseInt(req.query.num);
        pageSize = pageSize ? pageSize : 2;

        let order = req.query.order;
        let orderP = ['id', 'ASC'];
        if(order == '1'){
            orderP = ['id', 'DESC'];
        }

        models.student.findAndCountAll({
            offset: (page - 1) * pageSize,
            limit: pageSize,
            order: [
                orderP
            ]
        })
        .then(data =>{
            res.send({
                code: 0,
                mgs: 'OK',
                data: models.toArray(data.rows)
            })
        })
        .catch(err => {
            res.send(
                {
                    code: 1,
                    msg: 'Error',
                    data: []
                }
            );
        })
    }

    detail(req, res){
        let id = req.params.id;
        models.student.findOne({
            where: {
                id
            }
        })
        .then((result) => {
            res.send({
                code: 0,
                msg: 'OK',
                data: models.oneToObj(result)
            })
        }).catch((err) => {
            res.send({
                code: 1,
                msg: '您找的数据不存在',
                data: {}
            })
        });
    }

    update(req, res){
        // 要操作的用户的id
        let id = req.params.id;
        // 打算修改的数据
        // TODO 验证要修改的数据的合法性
        let data = req.body;
        
        if(!Object.keys(data).length){
            res.send({
                code: 1,
                msg: '参数有误'
            })
            return false;
        }
        models.student.findOne({
            where: {
                id
            }
        }).then(obj => {
            // 更新对象的信息
            
            for (const key in data) {
                console.log(obj.dataValues.hasOwnProperty(key), key);
                if (obj.dataValues.hasOwnProperty(key)) {
                    obj[key] = data[key];
                    
                }
            }
            console.log(models.oneToObj(obj));
            return obj.save();
        }).then(
            data => {
                res.send({
                        code: 0,
                        msg: 'OK'
                    })
            },
            err => {
                res.send({
                        code: 1,
                        msg: '更新失败'
                    })
            }
        )
    }

    add(req, res){
        let data = req.body;
        // TODO 数据验证

        models.student.create(data)
        .then(result => {
            res.send({
                code: 0,
                msg: 'OK'
            })
        })
        .catch(err => {
            res.send({
                code: 1,
                msg: err.message
            })
        })
    }

    delete(req, res){
        let id = req.params.id;

        models.student.destroy({
            where: { id }
        })
        .then(result => {
            res.send({
                code: 0,
                msg: 'OK'
            })
        })
        .catch(err => {
            res.send({
                code: 1,
                msg: '删除失败'
            })
        })
    }



}

module.exports = new student();